<?php
if (!defined('APP_ID')) { die('No direct script access allowed'); }

/**
 * Settings controller
 */
class Controller_Settings extends Controller_Template {
	protected $_template = 'other/master';

	/**
	 * Update current settings
	 */
	public function updateAction($error = -1) {
		if (Input::isPostBack()) {
			$success = false;
			if (!Settings::getCurrentId()) {
				$data = array();
				Query::data($data, 'insert_user_id', User::getId(), Query::DATA_INTEGER);
				Query::data($data, 'insert_time', time(), Query::DATA_INTEGER);
				if (Settings::insert($data)) {
					$success = true;
				}
			} else {			
				$data = array();
				Query::data($data, 'update_user_id', User::getId(), Query::DATA_INTEGER);
				Query::data($data, 'update_time', time(), Query::DATA_INTEGER);
				if (Settings::update($data)) {
					$success = true;
				}
			}

			if ($success) {
				Tools::redirect(CURRENT_URL . '0');
			} else {
				Tools::redirect(CURRENT_URL . '1');
			}
		} else {
			$this->template->content = View::factory('settings/update')
				->set('error', $error)
				->set('info', Settings::getInfo())
				->render();
		}
	}
}